namespace ASPNET.StarterKit.IssueTracker
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class DisplayCustomFields : UserControl
    {
        [AccessedThroughProperty("divider")]
        private HtmlContainerControl _divider;
        [AccessedThroughProperty("lstCustomFields")]
        private DataList _lstCustomFields;
        private object designerPlaceholderDeclaration;
        public bool Required;

        public DisplayCustomFields()
        {
            base.Init += new EventHandler(this.Page_Init);
            this.Required = true;
        }

        public override void DataBind()
        {
            this.lstCustomFields.DataKeyField = "Id";
            this.lstCustomFields.DataBind();
            if (this.lstCustomFields.Items.Count == 0)
            {
                this.divider.Visible = false;
            }
            else
            {
                this.divider.Visible = true;
            }
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        public void lstCustomFieldsItemDataBound(object s, DataListItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                CustomField currentField = (CustomField) e.Item.DataItem;
                Label lblFieldName = (Label) e.Item.FindControl("lblFieldName");
                lblFieldName.Text = currentField.Name;
                TextBox txtFieldValue = (TextBox) e.Item.FindControl("txtFieldValue");
                txtFieldValue.Text = currentField.Value;
                RequiredFieldValidator valReq = (RequiredFieldValidator) e.Item.FindControl("valReq");
                if (!this.Required)
                {
                    valReq.Visible = false;
                }
                else
                {
                    valReq.Visible = currentField.Required;
                }
                CompareValidator valCompare = (CompareValidator) e.Item.FindControl("valCompare");
                valCompare.Type = currentField.DataType;
                valCompare.Text = string.Format("({0})", currentField.DataType);
            }
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        public object DataSource
        {
            get
            {
                return this.lstCustomFields.DataSource;
            }
            set
            {
                this.lstCustomFields.DataSource = RuntimeHelpers.GetObjectValue(value);
            }
        }

        protected virtual HtmlContainerControl divider
        {
            get
            {
                return this._divider;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._divider != null)
                {
                }
                this._divider = value;
                if (this._divider != null)
                {
                }
            }
        }

        protected virtual DataList lstCustomFields
        {
            get
            {
                return this._lstCustomFields;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lstCustomFields != null)
                {
                    this._lstCustomFields.ItemDataBound -= new DataListItemEventHandler(this.lstCustomFieldsItemDataBound);
                }
                this._lstCustomFields = value;
                if (this._lstCustomFields != null)
                {
                    this._lstCustomFields.ItemDataBound += new DataListItemEventHandler(this.lstCustomFieldsItemDataBound);
                }
            }
        }

        public CustomFieldCollection Values
        {
            get
            {
                CustomFieldCollection colFields = new CustomFieldCollection();
                int _Vb_t_i4_0 = this.lstCustomFields.Items.Count - 1;
                for (int i = 0; i <= _Vb_t_i4_0; i++)
                {
                    DataListItem item = this.lstCustomFields.Items[i];
                    if ((item.ItemType == ListItemType.Item) | (item.ItemType == ListItemType.AlternatingItem))
                    {
                        int fieldId = IntegerType.FromObject(this.lstCustomFields.DataKeys[i]);
                        string fieldValue = ((TextBox) item.FindControl("txtFieldValue")).Text;
                        colFields.Add(new CustomField(fieldId, fieldValue));
                    }
                }
                return colFields;
            }
        }
    }
}

